package com.snakegame.ui;

import com.snakegame.core.GameManager;
import com.snakegame.config.GameState;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 游戏结束面板，显示游戏结束信息
 */
public class GameOverPanel extends JPanel {
    private GameManager gameManager;
    private GameFrame gameFrame;
    
    public GameOverPanel(GameManager gameManager, GameFrame gameFrame) {
        this.gameManager = gameManager;
        this.gameFrame = gameFrame;
        
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.insets = new Insets(10, 0, 10, 0);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.ipadx = 100;
        gbc.ipady = 10;
        
        // 游戏结束标题
        JLabel titleLabel = new JLabel("游戏结束");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 36));
        titleLabel.setForeground(Color.RED);
        gbc.gridy = 0;
        gbc.weighty = 0.3;
        add(titleLabel, gbc);
        
        // 分数显示
        JLabel scoreLabel = new JLabel("最终分数: " + gameManager.getScore());
        scoreLabel.setFont(new Font("宋体", Font.BOLD, 24));
        scoreLabel.setForeground(Color.WHITE);
        gbc.gridy = 1;
        gbc.weighty = 0;
        add(scoreLabel, gbc);
        
        // 重新开始按钮
        JButton restartButton = new JButton("重新开始");
        restartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.startGame();
                gameFrame.showPanel("GAME");
            }
        });
        gbc.gridy = 2;
        gbc.weighty = 0.3;
        add(restartButton, gbc);
        
        // 返回菜单按钮
        JButton menuButton = new JButton("返回菜单");
        menuButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setGameState(GameState.START_MENU);
                gameFrame.showPanel("START_MENU");
            }
        });
        gbc.gridy = 3;
        gbc.weighty = 0;
        add(menuButton, gbc);
        
        setBackground(Color.BLACK);
    }
    
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 更新分数显示
        Component[] components = getComponents();
        if (components.length >= 2 && components[1] instanceof JLabel) {
            ((JLabel) components[1]).setText("最终分数: " + gameManager.getScore());
        }
    }
}    